package touro.mancala.client.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;

import touro.mancala.client.GameState;
import touro.mancala.client.MoveListener;

public class MancalaFrame extends JFrame {

	private static final long serialVersionUID = 1002724596516834624L;

	public MancalaFrame(String ipAddress) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Mancala");
		
		GameState state = GameState.getGameState();
		MoveListener listener = new MoveListener();
		BoardView view = new BoardView();
		view.setMoveListener(listener);
		setContentPane(view);
		addComponentListener(new ResizeListener());
	}
	
	private class ResizeListener implements ComponentListener{

		@Override
		public void componentResized(ComponentEvent e) {
			MancalaFrame frame = (MancalaFrame)e.getSource();
			int newWidth = frame.getWidth();
			int newHeight = frame.getHeight();
			Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
			int y = (int)scrnSize.getHeight()-10;
			if (newWidth < 400) {
				frame.setSize(400, 400/8*6);
			} else if (newHeight > y) {
				frame.setSize(y/6*8, y);
			} else {
				frame.setSize(newWidth, newWidth/8*6);
			}
		}

		@Override
		public void componentHidden(ComponentEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void componentMoved(ComponentEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void componentShown(ComponentEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
